<?php

class MyFunctionCustom extends CActiveRecord {
    /*
     * DTOAN
     * 
     */

    public static $status = array(
        STATUS_INACTIVE => 'Inactive',
        STATUS_ACTIVE => 'Active',
    );

    public static function getStatus($value) {
        if (!is_null($value)) {
            $arrStatus = MyFunctionCustom::$status;
            if (array_key_exists($value, $arrStatus)) {
                return $arrStatus[$value];
            }
        }
    }

    public static function formatDate($value) {
        if (!is_null($value))
            return date('d-m-Y', strtotime($value));
        return '';
    }


    public static function getDropdownlistBrand() {
        $result = Brand::model()->findAll();
        if (count($result) > 0) {
            return CHtml::listData($result, 'BrandId', 'BrandName');
        }
        return array();
    }

    public static function getDropdownlistCategory() {
        $result = Category::model()->findAll();
        if (count($result) > 0) {
            return CHtml::listData($result, 'CategoryId', 'CategoryName');
        }
        return array();
    }
    
    public static function getDropdownlistSubCategory(){
        $result = SubCategory::model()->findAll();
        if (count($result) > 0) {
            return CHtml::listData($result, 'SubCateId', 'SubCateName');
        }
        return array();   
    }

    public static function getDropdownlistSubSubCategory(){
        $result = SubSubCategory::model()->findAll();
        if (count($result) > 0) {
            return CHtml::listData($result, 'SubSubCateId', 'SubSubCateName');
        }
        return array();   
    }
    
    public static function getDropdownlistGenre() {
        $result = Genre::model()->findAll();
        if (count($result) > 0) {
            return CHtml::listData($result, 'GenreId', 'GenreName');
        }
        return array();
    }

    public static function getDropdownlistColor() {
        $result = Color::model()->findAll();
        if (count($result) > 0) {
            return CHtml::listData($result, 'ColorId', 'ColorName');
        }
        return array();
    }

    public static function getDropdownlistSize() {
        $result = Size::model()->findAll();
        if (count($result) > 0) {
            return CHtml::listData($result, 'SizeId', 'SizeName');
        }
        return array();
    }

    public static function getDropdownlistPhoto() {
        $result = Photo::model()->findAll();
        if (count($result) > 0) {
            return CHtml::listData($result, 'PhotoId', 'Title');
        }
        return array();
    }

    public static function maxFileUpload() {
        return (5 * 1024 * 1024);
    }

    public static function saveMap($model, $objectFile) {
        $ImageProcessing = new ImageProcessing();
        $ImageProcessing->createDirectoryByPath('data/map');
        $ImageProcessing->folder = '/data/map';
        $objectFile->saveAs(Yii::app()->basePath . "/../data/map/$model->fileImage");
        return $model->fileImage;
    }

    public static function saveCover($model, $cover_image) {
        $ImageProcessing = new ImageProcessing();
        $ImageProcessing->createDirectoryByPath('images/cover');
        $ImageProcessing->folder = '/images/cover';
        $cover_image->saveAs(Yii::app()->basePath . "/../images/cover/$model->CoverPhoto");
        return $model->CoverPhoto;
    }

    public static function saveSlide($model, $cover_image) {
        $ImageProcessing = new ImageProcessing();
        $ImageProcessing->createDirectoryByPath('images/slide');
        $ImageProcessing->folder = '/images/slide';
        $cover_image->saveAs(Yii::app()->basePath . "/../images/slide/$model->SlidePhotoName");
        return $model->SlidePhotoName;
    }

    public static function DeleteFile($model, $fieldName, $folderName) {
        $ImageProcessing = new ImageProcessing();
        $ImageProcessing->folder = "/images/$folderName";
        if ($model->$fieldName != null) {
            $ImageProcessing->deleteFile($ImageProcessing->folder . '/' . $model->$fieldName);
        }
    // $cover_image->saveAs(Yii::app()->basePath . "/../data/cover/$model->cover_image");
    // return $model->cover_image;
    }   
    
    public static function autologin($modelUser){
        $modelLogin=new LoginForm();
        $modelLogin->UserName    = $modelUser->UserName;
        $modelLogin->Password    = $modelUser->Password;
        if($modelLogin->validate()){
            return true;
        }else{
            echo "<pre>";
            echo print_r($modelLogin->getErrors());
            echo "</pre>";
            die();
        }
            
        return false;
    } 
    
      public static function randString($length=6, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
    {
        $str = '';
        $count = strlen($charset);
        while ($length--) {
            $str .= $charset[mt_rand(0, $count-1)];
        }
        return $str;
    }

    public static function getPhotoByIdProduct($id){
            $criteria = new CDbCriteria();
            $criteria->select = 't.*';
            $criteria->condition = 't.Product = :productid and t.CoverPhoto="1"';
            $criteria->params = ':productid=>' . $id;
            $criteria->limit = '1';
            $product = new Product();
            $data = $product->findAll($criteria);
            return $data;
        }
    public static function countProductBySubSub($id){
        $result = Product::model()->findByAttributes(array('SubSubCateId' => $id));
        return count($result);
    }

   
    public static function countProductByBrand($id){
        $result = Product::model()->findByAttributes(array('BrandId' => $id));
        return count($result);
    }

    public static function convertNameRewriteToId($name){
        $result = SubCategory::model()->findByAttributes(array('SubCateNameRewrite' => $name));
        return $result->SubCateId;
    }

    public static function getMenu($Group){
        
        $model = new Menu();
        $criteria = new CDbCriteria();
        $criteria->select = 't.*';
        $criteria->condition = 't.GroupMenu="' . $Group.'"';
        $criteria->order = 't.Order ASC';
        $menu = $model->findAll($criteria);
        
        return $menu;
    }
    public static function getSubMenuByMenu($id){
        $model = new SubMenu();
        $criteria = new CDbCriteria();
        $criteria->select = 't.*';
        $criteria->condition = 't.Status="1" and t.MenuId="' . $id.'"';
        $criteria->order = 't.Order ASC';
        $menu = $model->findAll($criteria);
        
        return $menu;
    }
}

?>